package com.fingard.dsp.bank.directbank.cmbc02.util.cert;

import cmbc.cfca.sm2rsa.common.PKIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class CertConfig {
	private static Logger logger = LoggerFactory.getLogger(CertConfig.class);

	public static Cert merchantCert = new Cert();// 商户

	public static Map<String, Cert> merchantCertMap = new HashMap<String, Cert>();// 证书

	public static Cert getMerchantCert(String merchantNum) {
		Cert cert = merchantCertMap.get(merchantNum);
		if (cert == null) {
			cert = merchantCert;
			logger.info("[" + merchantNum + "]使用默认证书");
		}
		return cert;
	}

	public static boolean addMerchantCertInMap(String merchantNum, Cert cert) throws PKIException {
		if (cert.merCert != null && cert.merPublicKey != null && cert.merSm2Cert != null && cert.merPrivateKey != null) {
			if (cert.cmbcCert == null) {
				cert.cmbcCert = merchantCert.cmbcCert;// 民生公钥为空，取默认公钥
				cert.cmbcPublicKey = cert.cmbcCert.getPublicKey();
			}
			merchantCertMap.put(merchantNum, cert);
			logger.info("[" + merchantNum + "]添加证书成功");
			return true;
		} else {
			return false;
		}
	}

	public static boolean addDefaultMerchantCert(Cert cert) {
		if (cert.merCert != null && cert.merPublicKey != null && cert.merSm2Cert != null && cert.merPrivateKey != null && cert.cmbcCert != null
				&& cert.cmbcPublicKey != null) {
			merchantCert = cert;
			logger.info("添加默认证书成功");
			return true;
		} else {
			return false;
		}
	}
}
